package com.hyfd.task;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.hyfd.common.utils.ToolDateTime;
import com.hyfd.common.utils.ToolHttp;
import com.hyfd.common.utils.XiChengAESUtils;
import com.hyfd.common.utils.XiChengMD5Utils;
import com.hyfd.common.utils.XmlUtils;
import com.hyfd.dao.mp.OrderDao;
import com.hyfd.dao.mp.ProviderPhysicalChannelDao;
import com.hyfd.rabbitMq.RabbitMqProducer;
import com.hyfd.rabbitMq.SerializeUtil;

@Component
public class XiChengFlowTask
{
    
    Logger log = Logger.getLogger(getClass());
    
    @Autowired
    OrderDao orderDao;
    
    @Autowired
    RabbitMqProducer mqProducer;// 消息队列生产者
    
    @Autowired
    ProviderPhysicalChannelDao providerPhysicalChannelDao;// 物理通道信息
    
    // @Scheduled(fixedDelay = 60000)
    public void queryZhiTongOrder()
    {
        Map<String, Object> map = new HashMap<String, Object>();
        int flag = 2;
        try
        {
            String providerId = "0000000075";//
            Map<String, Object> channel = providerPhysicalChannelDao.selectByProviderId(providerId);// 获取通道
            String defaultParameter = (String)channel.get("default_parameter");// 参数
            Map<String, String> paramMap = XmlUtils.readXmlToMap(defaultParameter);
            String linkUrl = paramMap.get("queryUrl");// 查询地址
            String partyId = paramMap.get("partyId");// 身份ID
            String securitKey = paramMap.get("securitKey");// 加密key
            // 查询订单中属于西城
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("dispatcherProviderId", providerId);
            param.put("status", "1");
            List<Map<String, Object>> orderList = orderDao.selectByTask(param);// 查询出所有尚通的充值中的订单
            for (int i = 0, size = orderList.size(); i < size; i++)
            {
                Map<String, Object> order = orderList.get(i);
                String orderId = order.get("orderId") + "";// 平台订单号
                String providerOrderId = order.get("providerOrderId") + "";
                map.put("orderId", orderId);
                String time = ToolDateTime.format(new Date(), "yyyyMMddHHmmssSSS");// 发起请求的时间
                JSONObject jsonBody = new JSONObject();
                jsonBody.put("channelOrderId", providerOrderId);
                String data = XiChengAESUtils.encrypt(jsonBody.toString(), securitKey);
                // System.out.println("订单号--》》" + curids.length());
                JSONObject json = new JSONObject();
                String sign = XiChengMD5Utils.getSignAndMD5(partyId, data, time);
                json.put("partyId", partyId);
                json.put("time", time);
                json.put("data", data);
                json.put("sign", sign);
                
                String contentType = "application/x-www-form-urlencoded";
                // post请求
                String result = ToolHttp.post(false, linkUrl, json.toString(), contentType);
                // 判断是否成功
                if (result != null && !result.equals(""))
                {
                    JSONObject jObject = JSONObject.parseObject(result);
                    
                    String status = jObject.getString("status");
                    
                    if ("1".equals(status))
                    {
                        JSONObject dataJson = jObject.getJSONObject("data");
                        String channelOrderId = dataJson.getString("channelOrderId");
                        String orderStatus = dataJson.getString("orderStatus");
                        String resultCode = dataJson.getString("resultCode");
                        String resultDesc = dataJson.getString("resultDesc");
                        map.put("resultCode", resultCode + ":" + resultDesc);
                        map.put("providerOrderId", channelOrderId);
                        if ("1".equals(orderStatus))
                        {
                            flag = 1;
                        }
                        else if ("0".equals(orderStatus))
                        {
                            flag = 0;
                        }
                        else if ("2".equals(orderStatus))
                        {
                            continue;
                        }
                        else
                        {
                            flag = 0;
                        }
                    }
                    else
                    {
                        continue;
                    }
                }
                map.put("status", flag);
                mqProducer.sendDataToQueue(RabbitMqProducer.Result_QueueKey, SerializeUtil.getStrFromObj(map));
            }
        }
        catch (Exception e)
        {
            log.error("西城流量查询出错" + e);
        }
    }
}
